package lex.fractal.swing.common;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.lex.swing.Swings;


public class AnimatorControlPane extends JPanel {
	private JSlider delaySlider;
	private JButton stopButton;
	private JButton startButton;
	private FractalAnimator animator = null;
	private int value = 0;
	private int min = 0;
	private int max = 0;
	private int step = 0;

	public static void main(String[] args) {
		Swings.show(new AnimatorControlPane(null));
	}

	public AnimatorControlPane(FractalAnimator animator) {
		this(animator, FractalAnimator.DEFAULT_DELAY, FractalAnimator.MIN_DELAY, FractalAnimator.MAX_DELAY, FractalAnimator.STEP);
	}

	public AnimatorControlPane(FractalAnimator animator, int value, int min, int max, int step) {
		super();
		this.animator = animator;
		this.value = value;
		this.min = min;
		this.max = max;
		this.step = step;
		this.initialize();
	}

	private void start(boolean start) {
		if (true == start)
			this.animator.start();
		else
			this.animator.stop();
		getStartButton().setEnabled(!start);
		getStopButton().setEnabled(true);
		getDelaySlider().setEnabled(!start);
	}

	private JButton getStartButton() {
		if (startButton == null) {
			startButton = new JButton();
			startButton.setText("Start");
			startButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					start(true);
				}
			});
		}
		return startButton;
	}

	private JButton getStopButton() {
		if (stopButton == null) {
			stopButton = new JButton();
			stopButton.setText("Stop");
			stopButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					start(false);
				}
			});
		}
		return stopButton;
	}

	private JSlider getDelaySlider() {
		if (delaySlider == null) {
			delaySlider = new JSlider(JSlider.HORIZONTAL, min, max, value);
			delaySlider.setMajorTickSpacing(step * 4);
			delaySlider.setMinorTickSpacing(step);
			delaySlider.setPaintTicks(true);
			delaySlider.setPaintLabels(true);
			delaySlider.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					if (true == delaySlider.getValueIsAdjusting())
						return;
					animator.setDelay(delaySlider.getValue());
				}
			});
		}
		return delaySlider;
	}

	private void initialize() {
		final GroupLayout groupLayout = new GroupLayout((JComponent) this);
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								groupLayout
										.createParallelGroup(GroupLayout.Alignment.LEADING)
										.addComponent(getDelaySlider(), GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
										.addGroup(
												groupLayout.createSequentialGroup().addComponent(getStartButton())
														.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(getStopButton())))
						.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(getStartButton())
										.addComponent(getStopButton())).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(getDelaySlider(), GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(37, Short.MAX_VALUE)));
		setLayout(groupLayout);
	}
}
